<template>
	<view class="increased">
		<view class="from">
			<p><span>收货人</span><input type="text" v-model="from.username" placeholder="名字"></p>
			<p><span>手机号码</span><input type="number" :maxlength='11' v-model="from.mobile" placeholder="手机号"></p>
			<p @click='chooseLocation'><span>所在地址</span><i :class="{i:from.address}">{{from.address?from.address:'省、市、区、街道'}}</i></p>
			<p><span>详细地址</span><input type="text" v-model="from.house" placeholder="小区名称、楼栋/乡村名称"></p>
			<p style="border-bottom: 0;">设为默认收获地址<u-switch v-model="from.is" activeColor="#5ac725"></u-switch>
			</p>
		</view>
		<button @click="setAddress">保存</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				from: {
					username: '',
					mobile: '',
					address: '',
					is:0,
					is_default: false,
					lng: '',
					lat: '',
					house: '',
					flag:false,
				},
			};
		},
		onLoad(option) {
			this.from.flag = option.flag;
			if(uni.getStorageSync('xgdz')){
				let dz=JSON.parse(uni.getStorageSync('xgdz'))
				this.from.username=dz.username
				this.from.mobile=dz.mobile
				this.from.address=dz.address
				this.from.house=dz.house
				this.from.lng=dz.lng
				this.from.lat=dz.lat
				this.from.id=dz.id
				this.from.is=Boolean(dz.is_default)
				this.from.is_default=dz.is_default
			}
		},
		methods: {
			chooseLocation() {
				let that = this
				uni.chooseLocation({
					success: (res) => {
						this.from.address = res.address + res.name
						this.from.lng = res.longitude
						this.from.lat = res.latitude
					}
				});
			},
			setAddress() {
				this.from.is_default=Number(this.from.is)
				this.$httpRequest('/address/setAddress', this.from).then(() => {
					uni.redirectTo({
						url:'/pages/user/list/address/address?flag='+this.from.flag
					})
				})
			}
		},
	}
</script>

<style lang="less">
	.increased {
		&>.from {
			width: 690rpx;
			background: #fff;
			margin: 40rpx auto 0;
			display: flex;
			flex-direction: column;
			padding: 20rpx;
			box-sizing: border-box;
			border-radius: 20rpx;

			&>p {
				width: 100%;
				height: 100rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				border-bottom: 1px solid #F0F0F0;
				font-size: 15px;
				
				&>.i{
					color: #333;
				}
				
				&>i{
					flex: 1;
					height: 100%;
					display: flex;
					align-items: center;
					font-style: normal;
					font-size: 15px;
					color: #807e7e;
				}

				&>span {
					width: 214rpx;
				}

				&>input {
					flex: 1;
					height: 100%;
					font-size: 15px;
				}
			}
		}

		&>button {
			width: 690rpx;
			height: 80rpx;
			line-height: 80rpx;
			border-radius: 50rpx;
			color: #fff;
			background: #5FC07B;
			font-size: 15px;
			margin-top: 72rpx;
		}
	}
</style>